//
//  UIView+Shake.m
//  NuoDun_Customer
//
//  Created by Avidya on 2019/3/27.
//  Copyright © 2019 Avidya. All rights reserved.
//

#import "UIView+Shake.h"
#import <objc/runtime.h>
#import <sys/utsname.h>

static char kViewEventsBlockKey;

@implementation UIView (Shake)

- (void)shake {
    CAKeyframeAnimation *keyFrame = [CAKeyframeAnimation animationWithKeyPath:@"position.x"];
    keyFrame.duration = 0.3;
    CGFloat x = self.layer.position.x;
    keyFrame.values = @[@(x - 6), @(x - 6), @(x + 4), @(x - 4), @(x + 3), @(x - 3), @(x + 2), @(x - 2)];
    [self.layer addAnimation:keyFrame forKey:@"shake"];
}


+ (CGFloat)heightWithString:(NSString *)string labelWidth:(CGFloat)labelWidth fontSize:(CGFloat)fontSize {
    
    CGRect rect = [string boundingRectWithSize:CGSizeMake(labelWidth, MAXFLOAT)
                                       options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesDeviceMetrics | NSStringDrawingUsesLineFragmentOrigin
                                    attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]}
                                       context:nil];
    
    return rect.size.height;
}

+ (CGFloat)heightWithString:(NSString *)string labelWidth:(CGFloat)labelWidth fontBoldSize:(CGFloat)fontBoldSize {
    
    CGRect rect = [string boundingRectWithSize:CGSizeMake(labelWidth, MAXFLOAT)
                                       options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesDeviceMetrics | NSStringDrawingUsesLineFragmentOrigin
                                    attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:fontBoldSize]}
                                       context:nil];
    
    return rect.size.height;
}


+ (CGFloat)widthWithString:(NSString *)string labelHeight:(CGFloat)labelHeight fontSize:(CGFloat)fontSize {
    
    CGRect rect = [string boundingRectWithSize:CGSizeMake(MAXFLOAT, labelHeight)
                                       options:NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin
                                    attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]}
                                       context:nil];
    
    return rect.size.width;
}

- (void)handleViewControlEvents:(UIControlEvents)controlEvents withBlock:(id)block {
    
    objc_setAssociatedObject(self, &kViewEventsBlockKey, block, OBJC_ASSOCIATION_COPY_NONATOMIC);
    UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewSelect:)];
    [self addGestureRecognizer:tgr];
}

- (void)viewSelect:(id)sender {
    ActionBlock block = (ActionBlock)objc_getAssociatedObject(self, &kViewEventsBlockKey);
    if (block) {
        block();
    }
}

+ (NSString *)systemModel {
    
    struct utsname systemInfo;
    
    uname(&systemInfo);
    
    NSString *phoneType = [NSString stringWithCString: systemInfo.machine encoding:NSASCIIStringEncoding];
    
    // simulator 模拟器
    if ([phoneType isEqualToString:@"i386"])   return @"Simulator";
    
    if ([phoneType isEqualToString:@"x86_64"])  return @"Simulator";
    
    if ([phoneType isEqualToString:@"iPad"])  return @"iPad";
    
    //  常用机型
    if([phoneType  isEqualToString:@"iPhone1,1"])  return @"iPhone_2G";
    
    if([phoneType  isEqualToString:@"iPhone1,2"])  return @"iPhone_3G";
    
    if([phoneType  isEqualToString:@"iPhone2,1"])  return @"iPhone_3GS";
    
    if([phoneType  isEqualToString:@"iPhone3,1"])  return @"iPhone_4";
    
    if([phoneType  isEqualToString:@"iPhone3,2"])  return @"iPhone_4";
    
    if([phoneType  isEqualToString:@"iPhone3,3"])  return @"iPhone_4";
    
    if([phoneType  isEqualToString:@"iPhone4,1"])  return @"iPhone_4S";
    
    if([phoneType  isEqualToString:@"iPhone5,1"])  return @"iPhone_5";
    
    if([phoneType  isEqualToString:@"iPhone5,2"])  return @"iPhone_5";
    
    if([phoneType  isEqualToString:@"iPhone5,3"])  return @"iPhone_5c";
    
    if([phoneType  isEqualToString:@"iPhone5,4"])  return @"iPhone_5c";
    
    if([phoneType  isEqualToString:@"iPhone6,1"])  return @"iPhone_5s";
    
    if([phoneType  isEqualToString:@"iPhone6,2"])  return @"iPhone_5s";
    
    if([phoneType  isEqualToString:@"iPhone7,1"])  return @"iPhone_6_Plus";
    
    if([phoneType  isEqualToString:@"iPhone7,2"])  return @"iPhone_6";
    
    if([phoneType  isEqualToString:@"iPhone8,1"])  return @"iPhone_6s";
    
    if([phoneType  isEqualToString:@"iPhone8,2"])  return @"iPhone_6s_Plus";
    
    if([phoneType  isEqualToString:@"iPhone8,4"])  return @"iPhone_SE";
    
    if([phoneType  isEqualToString:@"iPhone9,1"])  return @"iPhone_7";
    
    if([phoneType  isEqualToString:@"iPhone9,2"])  return @"iPhone_7_Plus";
    
    if([phoneType  isEqualToString:@"iPhone10,1"]) return @"iPhone_8";
    
    if([phoneType  isEqualToString:@"iPhone10,4"]) return @"iPhone_8";
    
    if([phoneType  isEqualToString:@"iPhone10,2"]) return @"iPhone_8_Plus";
    
    if([phoneType  isEqualToString:@"iPhone10,5"]) return @"iPhone_8_Plus";
    
    if([phoneType  isEqualToString:@"iPhone10,3"]) return @"iPhone_X";
    
    if([phoneType  isEqualToString:@"iPhone10,6"]) return @"iPhone_X";
    
    if([phoneType  isEqualToString:@"iPhone11,8"]) return @"iPhone_XR";
    
    if([phoneType  isEqualToString:@"iPhone11,2"]) return @"iPhone_XS";
    
    if([phoneType  isEqualToString:@"iPhone11,4"]) return @"iPhone_XS_Max";
    
    if([phoneType  isEqualToString:@"iPhone11,6"]) return @"iPhone_XS_Max";
    
    if([phoneType  isEqualToString:@"iPhone12,1"])  return @"iPhone_11";
    
    if ([phoneType isEqualToString:@"iPhone12,3"])  return @"iPhone_11_Pro";
    
    if ([phoneType isEqualToString:@"iPhone12,5"])   return @"iPhone_11_Pro_Max";
    
    if ([phoneType isEqualToString:@"iPhone12,8"])   return @"iPhone_SE2";
    
    if ([phoneType isEqualToString:@"iPhone13,1"])   return @"iPhone_12_mini";
    
    if ([phoneType isEqualToString:@"iPhone13,2"])   return @"iPhone_12";
    
    if ([phoneType isEqualToString:@"iPhone13,3"])   return @"iPhone_12_Pro";
    
    if ([phoneType isEqualToString:@"iPhone13,4"])   return @"iPhone_12_Pro Max";
    
    if ([phoneType isEqualToString:@"iPhone14,4"])   return @"iPhone_13_mini";
    
    if ([phoneType isEqualToString:@"iPhone14,5"])   return @"iPhone_13";
    
    if ([phoneType isEqualToString:@"iPhone14,2"])   return @"iPhone_13_Pro";
    
    if ([phoneType isEqualToString:@"iPhone14,3"])   return @"iPhone_13_Pro_Max";
    
    if ([phoneType isEqualToString:@"iPhone14,6"])   return @"iPhone_SE3";
    
    if ([phoneType isEqualToString:@"iPhone14,7"])   return @"iPhone_14";
    
    if ([phoneType isEqualToString:@"iPhone14,8"])   return @"iPhone_14_Plus";
    
    if ([phoneType isEqualToString:@"iPhone15,2"])   return @"iPhone_14_Pro";
    
    if ([phoneType isEqualToString:@"iPhone15,3"])   return @"iPhone_14_Pro_Max";
    
    if ([phoneType isEqualToString:@"iPhone15,4"])   return @"iPhone_15";
    
    if ([phoneType isEqualToString:@"iPhone15,5"])   return @"iPhone_15_Plus";
    
    if ([phoneType isEqualToString:@"iPhone16,1"])   return @"iPhone_15_Pro";
    
    if ([phoneType isEqualToString:@"iPhone16,2"])   return @"iPhone_15_Pro_Max";
    
    if ([phoneType isEqualToString:@"iPhone17,1"])   return @"iPhone_16_Pro";
    
    if ([phoneType isEqualToString:@"iPhone17,2"])   return @"iPhone_16_Pro_Max";
    
    if ([phoneType isEqualToString:@"iPhone17,3"])   return @"iPhone_16";
    
    if ([phoneType isEqualToString:@"iPhone17,4"])   return @"iPhone_16_Plus";
    

    return phoneType;
}

+ (BOOL)isIphoneX {
    
//    if ([[UIView systemModel] isEqualToString:@"Simulator"]) return NO;
    
    if ([[UIView systemModel] isEqualToString:@"iPhone_2G"]) return NO;
    
    if ([[UIView systemModel] isEqualToString:@"iPhone_3G"]) return NO;
    
    if ([[UIView systemModel] isEqualToString:@"iPhone_3GS"]) return NO;
    
    if ([[UIView systemModel] isEqualToString:@"iPhone_4"]) return NO;
    
    if ([[UIView systemModel] isEqualToString:@"iPhone_4S"]) return NO;
    
    if ([[UIView systemModel] isEqualToString:@"iPhone_5"]) return NO;
    
    if ([[UIView systemModel] isEqualToString:@"iPhone_5c"]) return NO;
    
    if ([[UIView systemModel] isEqualToString:@"iPhone_5s"]) return NO;
    
    if ([[UIView systemModel] isEqualToString:@"iPhone_6_Plus"]) return NO;
    
    if ([[UIView systemModel] isEqualToString:@"iPhone_6"]) return NO;
    
    if ([[UIView systemModel] isEqualToString:@"iPhone_6s"]) return NO;
    
    if ([[UIView systemModel] isEqualToString:@"iPhone_6s_Plus"]) return NO;
    
    if ([[UIView systemModel] isEqualToString:@"iPhone_7"]) return NO;
    
    if ([[UIView systemModel] isEqualToString:@"iPhone_7_Plus"]) return NO;
    
    if ([[UIView systemModel] isEqualToString:@"iPhone_8"]) return NO;
    
    if ([[UIView systemModel] isEqualToString:@"iPhone_8_Plus"]) return NO;
    
    if ([[UIView systemModel] isEqualToString:@"iPad"]) return NO;
    
    
    
    return YES;
}



@end
